package com.hxzy.controller;

import com.hxzy.pojo.Emp;
import com.hxzy.pojo.PageBean;
import com.hxzy.pojo.Result;
import com.hxzy.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author wzm
 * @create 2024-03-04 15:33
 * project_name:JavaWeb2401
 * @Description
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    //该接口用于员工列表数据的条件分页查询(暂时只查分页)  纯手动分页
   /* @GetMapping //RequestParam当没有当前页和现实条数是使用默认值
    public Result page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询的参数 页码：{}，条数{}",page,pageSize);
        //调用service完成功能
        PageBean pageBean = empService.page(page,pageSize);
        return  Result.success(pageBean);//list+total
    }*/


/*    @GetMapping // 使用pageHelper进行分页
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize
    ){
        log.info("分页查询的参数 页码：{}，条数{}",page,pageSize);
        //调用service完成功能
        PageBean pageBean = empService.pageByHelper(page,pageSize);
        return  Result.success(pageBean);//list+total

    }*/

    //带条件的分页查询
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name,Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ){
        log.info("分页查询的参数 页码：{}，条数{},name:{},gender:{},begin:{},end：{}",page,pageSize,name,gender,begin,end);
        //调用service完成功能
        PageBean pageBean = empService.pageByHelper(page,pageSize,name,gender,begin,end);
        return  Result.success(pageBean);//list+total

    }

    //删除员工的接口
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        empService.deleteByIds(ids);
        return Result.success();
    }

    //添加员工的接口
    @PostMapping
    public Result add(@RequestBody Emp emp){
        log.info("添加的信息，{}",emp);
        empService.add(emp);
        return Result.success();
    }

    //根据id获取修改前员工信息
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("根据id查询员工数据{}",id);
       Emp emp = empService.findById(id);
       return Result.success(emp);

    }


    //根据id修改员工信息
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工信息，{}",emp);
        empService.update(emp);
        return Result.success();
    }






}
